package com.cn.wanxi.service;

import com.cn.wanxi.IO.ProductIO;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.ReadNote;
import com.cn.wanxi.util.Tool;

import java.util.ArrayList;
import java.util.List;

public class ProductService {

    /**
     * 得到公司信息里面的轮播数据(封装轮播数据)
     *
     * @return
     */
    public List<ProductModel> getCompanyPageProductList() {
        List<ProductModel> list = new ArrayList<>();
        ProductModel productModel = new ProductModel();//调用产品的轮播数组
        int length = productModel.getImgHrefs().length;//得到产品轮播数组的长度，我们需要便利次数
        for (int i = 0; i < length; i++) {
            ProductModel model = new ProductModel();//封裝产品信息
            model.setImgHref(productModel.getImgHrefs()[i]);
            list.add(model);
        }
        return list;
    }

    /**
     * 产品页面的所有信息
     *
     * @return
     */
    public List<ProductModel> getProductList() {

        ProductModel productModel = new ProductModel();
        String[] imgs = productModel.getImgsForProductPage();
        String[] titles = productModel.getTitlesForProductPage();
        Double[] aa = productModel.getPricesForProductPage();
        String[] prices = {};
        for (int i = 0; i < aa.length; i++) {
            prices[i] = aa[i].toString();
        }
        String[] contents = productModel.getContentsForProductPage();
        return setDataForProductPage(imgs, titles, prices, contents);
    }

    /**
     * 封装网站首页的产品信息
     *
     * @return
     */
    public List<ProductModel> getProductListForHome() {

        ProductModel productModel = new ProductModel();
        String imgs[] = productModel.getImgsForHome();
        String titles[] = productModel.getTitlesForHome();
        String contnets[] = productModel.getContentForHome();

        return setDataForHome(imgs, titles, contnets);
    }

    public List<ProductModel> getProductListForHomeFromIO() {
//        1.写入记事本
        ProductIO productIO = new ProductIO();
        productIO.writeNote(Tool.productForHomePath.getPath());
//        2.读取数据
        String string = ReadNote.read(Tool.productForHomePath.getPath());
//        3.封装数据
        return getStringToArrayForHome(string);
    }

    public List<ProductModel> getStringToArrayForHome(String string) {
        String[] temp = string.split(Tool.split.getPath());
        String img = temp[0];
        String title = temp[1];
        String content = temp[2];
        img = Tool.replace(img);
        title = Tool.replace(title);
        content = Tool.replace(content);
        String imgs[] = img.split(",");
        String titles[] = title.split(",");
        String contnets[] = content.split(",");
        return setDataForHome(imgs, titles, contnets);
    }

    private List<ProductModel> setDataForHome(String[] imgs, String[] titles, String[] contents) {
        List<ProductModel> list = new ArrayList<>();
        for (int i = 0; i < imgs.length; i++) {
            ProductModel model = new ProductModel();
            model.setImgHref(imgs[i]);
            model.setContent(contents[i]);
            model.setTitle(titles[i]);
            list.add(model);
        }
        return list;
    }

    /**
     * 得到记事本里面的数据
     *
     * @return
     */
    public List<ProductModel> getProductListFromIO() {
//        1.写入记事本
        ProductIO productIO = new ProductIO();
        productIO.writeNoteForProduct(Tool.productPagePath.getPath());
//        2.读取记事本数据
        String string = ReadNote.read(Tool.productPagePath.getPath());

//        3.封装数据
        return getStringToArrayForProductPage(string);
    }

    private List<ProductModel> getStringToArrayForProductPage(String string) {
        String[] temp = string.split(Tool.split.getPath());
        String img = temp[0];
        String title = temp[1];
        String price = temp[2];
        String content = temp[3];
        img = Tool.replace(img);
        title = Tool.replace(title);
        price = Tool.replace(price);
        content = Tool.replace(content);
        String[] imgs = img.split(",");
        String[] titles = title.split(",");
        String[] prices = price.split(",");
        String[] contents = content.split(",");
        return setDataForProductPage(imgs, titles, prices, contents);
    }

    /**
     * 封装数据
     *
     * @param imgs
     * @param titles
     * @param prices
     * @param contents
     * @return
     */
    private List<ProductModel> setDataForProductPage(String[] imgs, String[] titles, String[] prices, String[] contents) {
        List<ProductModel> list = new ArrayList<>();

        for (int i = 0; i < imgs.length; i++) {
            ProductModel model = new ProductModel();
            model.setImgHref(imgs[i]);
            model.setTitle(titles[i]);
            model.setPrice(Double.parseDouble(prices[i]));
            model.setContent(contents[i]);
            list.add(model);
        }
        return list;
    }
}
